<template>
  <div class="login-container">

      <div  v-if="nameandtoken ? false : true" class="login-box">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
          <el-form-item class="Form-item">
            <div class="number1">欢迎加入codeliu的小家庭!</div>
          </el-form-item>
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input class="input" v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input class="input" v-model="loginForm.password"  prefix-icon="iconfont icon-3702mima" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="Form-item">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button  @click="register">新手</el-button>
          </el-form-item>
          <el-form-item class="Form-item">
            <div style="color: red">提示：如果没有帐号可以点击新手前往注册，欢迎你的加入</div>
          </el-form-item>
        </el-form>
      </div>
      <div v-else class="havelogin-box">
        <span class="logout-tishi">
          <div>
            主人，您已经登陆过啦
          </div>
          <div>
            可在个人信息退出哦
          </div>
        </span>
        <img @click="logout" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602776137223&di=3470ce3d58f56706ad3146887b5557b4&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F12410f9f7d1dda70a183c291a31b11947505afc0278b66-D0Wmv0_fw658" alt="">
      </div>


  </div>
</template>

<script>
export default {
  data () {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: ''
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
      nameandtoken: ''
    }
  },
  created() {
    if (window.sessionStorage.getItem('token')) {
      this.nameandtoken = true
    }
    else {
      this.nameandtoken = false
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.error({
          message: '表单校验失败！',
          center: true
        })
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.status !== 200) return this.$message.error({
          message: '用户名或密码错误！',
          center: true
        })
        this.$message.success({
          message: '登陆成功！',
          center: true
        })
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        this.$store.commit('changeProfile',res)
        window.sessionStorage.setItem('token', res.token)
        window.sessionStorage.setItem('name', res.name)
        window.sessionStorage.setItem('idiograph', res.idiograph)
        window.sessionStorage.setItem('username', res.username)
        window.sessionStorage.setItem('userNumber', res.lenght)
        window.sessionStorage.setItem('Interview', res.count)
        window.sessionStorage.setItem('articleThumbs', res.articleThumbs)
        if (res.picture == '') {
          window.sessionStorage.setItem('picture', './touxiang/1.jpg')
        }else {
          window.sessionStorage.setItem('picture', res.picture)
        }
        // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/profile')
      })
    },
    register () {
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="less" scoped>
.login-container{
  .havelogin-box{
    color: orange;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 40px;
    margin-top: 40vh;
    img{
      width: 200px;
      height: 200px;
    }
  }

  .login-box{
    width: 50vw;
    height: 35rem;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    margin-top: 40vh;
    .login_form{
      padding: 1rem;
      .Form-item{
        text-align: center;
        .number1{
          font-size: 3rem;
          color: orange;
        }
        .login-btn{
          margin-right: 2rem;
        }
    }
    }
  }
  .logout-tishi{
    width: 110%;
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 21px;
    background-color: #fff;
  }
  @media (min-width: 992px) and (max-width: 1300px) {
   .login-box{
     width: 80%;
   }

  }
  @media (min-width: 768px) and (max-width: 991px) {
    .login-box{
      width: 80%;
    }

  }
  @media (max-width: 767px) {
    .login-box{
      width: 90%;
      .login_form{
        .Form-item{
          .number1{
            font-size: 2.5rem;
            color: orange;
          }
          .login-btn{
          }
        }
      }
    }
  }
}
</style>
